package com.erp.collect.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.utils.oldUtils.StringUtils;
import com.ruoyi.common.core.web.oldPage.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.erp.collect.domain.bo.ErpKakaImgFenliBo;
import com.erp.collect.domain.vo.ErpKakaImgFenliVo;
import com.erp.collect.domain.ErpKakaImgFenli;
import com.erp.collect.mapper.ErpKakaImgFenliMapper;
import com.erp.collect.service.IErpKakaImgFenliService;

import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.Objects;

/**
 * kaka采集图片分离Service业务层处理
 *
 * @author kaka
 * @date 2024-05-21
 */
@RequiredArgsConstructor
@Service
public class ErpKakaImgFenliServiceImpl implements IErpKakaImgFenliService {

    private final ErpKakaImgFenliMapper baseMapper;

    /**
     * 查询kaka采集图片分离
     */
    @Override
    public ErpKakaImgFenliVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询kaka采集图片分离列表
     */
    @Override
    public TableDataInfo<ErpKakaImgFenliVo> queryPageList(ErpKakaImgFenliBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ErpKakaImgFenli> lqw = buildQueryWrapper(bo);
        Page<ErpKakaImgFenliVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询kaka采集图片分离列表
     */
    @Override
    public List<ErpKakaImgFenliVo> queryList(ErpKakaImgFenliBo bo) {
        LambdaQueryWrapper<ErpKakaImgFenli> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ErpKakaImgFenli> buildQueryWrapper(ErpKakaImgFenliBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ErpKakaImgFenli> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getPath()), ErpKakaImgFenli::getPath, bo.getPath());
        lqw.eq(StringUtils.isNotBlank(bo.getJson()), ErpKakaImgFenli::getJson, bo.getJson());
        lqw.eq(StringUtils.isNotBlank(bo.getUrl()), ErpKakaImgFenli::getUrl, bo.getUrl());
        lqw.eq(StringUtils.isNotBlank(bo.getRepath()), ErpKakaImgFenli::getRepath, bo.getRepath());
        return lqw;
    }

    /**
     * 新增kaka采集图片分离
     */
    @Override
    public Boolean insertByBo(ErpKakaImgFenliBo bo) {
        ErpKakaImgFenli add = BeanUtil.toBean(bo, ErpKakaImgFenli.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改kaka采集图片分离
     */
    @Override
    public Boolean updateByBo(ErpKakaImgFenliBo bo) {
        ErpKakaImgFenli update = BeanUtil.toBean(bo, ErpKakaImgFenli.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ErpKakaImgFenli entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除kaka采集图片分离
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * @Description: 根据旧图片链接，获取新图片链接
     * @Author: j.zh
     * @Date: 2024/5/21
     * @param url
     * @return
     * @Version: 4.0.0
     **/
    @Override
    public String getRePathByOldUrl(String url){
        ErpKakaImgFenli erpKakaImgFenli = baseMapper.selectOne(new LambdaQueryWrapper<ErpKakaImgFenli>()
                .eq(ErpKakaImgFenli::getUrl, url).last("limit 1"));
        if (Objects.isNull(erpKakaImgFenli)){
            return null;
        }else {
            return erpKakaImgFenli.getRepath();
        }
    }
}
